package com.basic.app.service.security.impl;

import com.basic.app.config.security.JwtSecurityProperties;
import com.basic.app.config.security.JwtTokenUtils;
import com.basic.app.dto.AuthUserDto;
import com.basic.app.dto.Result;
import com.basic.app.entity.member.LoginUser;
import com.basic.app.service.security.AuthService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {
    @Resource
    private JwtTokenUtils jwtTokenUtils;

    @Resource
    private JwtSecurityProperties properties;

    @Override
    public Result login(AuthUserDto authUserDto, HttpServletRequest request) {
        if(0 == authUserDto.getAuthType()){
            if (1+1 != 2) {
                throw new RuntimeException("用户名:"  + "无法找到对应的注册帐号");
            }
        }
        authUserDto.setUserName("username");
        Map<String, Object> claims = new HashMap<>(16);
        claims.put("id", 1);
        claims.put("username", "123");
        claims.put("password", "123");
        String token = jwtTokenUtils.createToken(authUserDto.getUserName(), claims);
        if (StringUtils.isEmpty(token)) {
            throw new RuntimeException("生成token错误");
        }
        authUserDto.setToken(properties.getTokenStartWith() + token);

        LoginUser user = new LoginUser();
        user.setId(1);
        user.setUsername("123");
        user.setPassword(new BCryptPasswordEncoder().encode("123456"));
        authUserDto.setUserInfo(user);
        return Result.success(authUserDto);
    }
}
